package linkedlist.test61;

import listbyorder.utils.ListNode;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/14 13:48
 */
public class Solution1 {

    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null) return head;
        // 遍历除链表的长度
        ListNode t = head;
        int l = 1;
        while (t.next != null) {
            t = t.next;
            l++;
        }
        t.next = head;  // 构成环
        k = k % l;
        k = l - k - 1;
        t = head;
        while (k-- != 0) {
            t = t.next;
        }
        ListNode ans = t.next;
        t.next = null;
        return ans;
    }
}
